#include "dlg_book_addandupdate.h"
#include "ui_dlg_book_addandupdate.h"
#include "lib/sqlmgr.h"

Dlg_Book_AddAndUpdate::Dlg_Book_AddAndUpdate(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_Book_AddAndUpdate)
{
    ui->setupUi(this);
}

Dlg_Book_AddAndUpdate::~Dlg_Book_AddAndUpdate()
{
    delete ui;
}

void Dlg_Book_AddAndUpdate::setType(int id)
{
    m_id = id;
    //执行sql获取信息显示到对应位置
    auto l = SqlMgr::getInstance()->getBooks(QString("where bookid = %1").arg(id));
    if(l.size()==1){
        QStringList data = l[0];
        ui->le_name->setText(data[1]);
        ui->le_press->setText(data[2]);
        ui->type1->setCurrentText(data[3]);
        ui->type2->setCurrentText(data[4]);
        ui->type3->setCurrentText(data[5]);
        ui->le_cnt->setText(data[6]);
    }
}

void Dlg_Book_AddAndUpdate::on_btn_ok_clicked()
{
    QStringList l;
    l<<QString::number(m_id);
    l<<ui->le_name->text();
    l<<ui->le_press->text();
    l<<ui->type1->currentText();
    l<<ui->type2->currentText();
    l<<ui->type3->currentText();
    l<<ui->le_cnt->text();
    l<<"";
    if(m_id!=-1){
        //更新
        SqlMgr::getInstance()->updateBooks(l);
    }else{
        //添加
        QVector<QStringList> vec;
        vec.push_back(l);
        SqlMgr::getInstance()->addBook(vec);
    }
    this->hide();
}


void Dlg_Book_AddAndUpdate::on_btn_cancel_clicked()
{
    this->hide();
}

